package com.zyd.blog.controller;

import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.entity.ArticleFavor;
import com.zyd.blog.business.entity.CaredUser;
import com.zyd.blog.business.entity.User;
import com.zyd.blog.business.service.BizArticleFavorService;
import com.zyd.blog.business.service.BizCareUserService;
import com.zyd.blog.business.vo.ArticleFavorConditionVO;
import com.zyd.blog.business.vo.CaredUserConditionVO;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.framework.object.ResponseVO;
import com.zyd.blog.util.ResultUtil;
import com.zyd.blog.util.SessionUtil;
import com.zyd.blog.util.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 收藏
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/careUser")
public class RestCaredUserController {
    @Autowired
    private BizCareUserService bizCareUserService;

    @PostMapping("/add/{id}")
    public ResponseVO add(@PathVariable("id") Long id) {
        CaredUser caredUser = new CaredUser();
        caredUser.setCaredUserId(id);
        User loginUser = SessionUtil.getUser();
        if(loginUser != null) {
            if(loginUser.getId() != null) {
                caredUser.setUserId(loginUser.getId());
            }
        }else {
            return ResultUtil.error("操作失败");
        }
        caredUser.setId(SnowflakeIdWorker.getWorker().nextId());
        CaredUser cu = bizCareUserService.insert(caredUser);
        return ResultUtil.success(cu);
    }

    @PostMapping("/delete/{id}")
    public ResponseVO delete(@PathVariable("id") Long id) {
        CaredUser caredUser = new CaredUser();
        caredUser.setCaredUserId(id);
        User loginUser = SessionUtil.getUser();
        if(loginUser != null) {
            if(loginUser.getId() != null) {
                caredUser.setUserId(loginUser.getId());
            }
        }else {
            return ResultUtil.error("操作失败");
        }
        CaredUser cu = bizCareUserService.delete(caredUser);
        return ResultUtil.success(cu);
    }

    @PostMapping("/list")
    public PageResult list(CaredUserConditionVO vo) {
        PageInfo<CaredUser> pageInfo = bizCareUserService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }
}
